package com.xww.common.base;

import com.xww.common.exception.CommonError;

import java.io.Serializable;

public class ResultData<T> implements Serializable{

        private static final long serialVersionUID = -1L;

        public static final int SUCCESS = 1;

        public static final int FAIL = 0;

        private int status;

        private long count;

        private String errCode;

        private String errMsg;

        private T data;

        public int getStatus() {
                return status;
        }

        public void setStatus(int status) {
                this.status = status;
        }

        public long getCount() {
                return count;
        }

        public void setCount(long count) {
                this.count = count;
        }

        public String getErrCode() {
                return errCode;
        }

        public void setErrCode(String errCode) {
                this.errCode = errCode;
        }

        public String getErrMsg() {
                return errMsg;
        }

        public void setErrMsg(String errMsg) {
                this.errMsg = errMsg;
        }

        public T getData() {
                return data;
        }

        public void setData(T data) {
                this.data = data;
        }

        @Override
        public String toString() {
                return "ResultData{" +
                        "status=" + status +
                        ", count=" + count +
                        ", errCode='" + errCode + '\'' +
                        ", errMsg='" + errMsg + '\'' +
                        ", data=" + data +
                        '}';
        }

        public ResultData() {
        }
        /**
         * 只有状态
         * @param status
         */
        public ResultData(int status) {
                this.status = status;
        }

        /**
         * 有状态有数据
         * @param status
         * @param data
         */
        public ResultData(int status, T data) {
                this.status = status;
                this.data = data;
        }

        /**
         * 有状态,有错误码,有错误信息
         * @param status
         * @param errCode
         * @param errMsg
         */
        public ResultData(int status, String errCode, String errMsg) {
                this.status = status;
                this.errCode = errCode;
                this.errMsg = errMsg;
        }


        /**
         * 成功
         * @return
         */
        public static ResultData success(){
                ResultData resultData = new ResultData();
                resultData.setStatus(ResultData.SUCCESS);
                return resultData;
        }
        /**
         * 成功
         * @return
         */
        public static ResultData success(Object data){
                ResultData resultData = new ResultData();
                resultData.setStatus(ResultData.SUCCESS);
                resultData.setData(data);
                return resultData;
        }

        public static ResultData success(long count,Object data){
                ResultData resultData = new ResultData();
                resultData.setStatus(ResultData.SUCCESS);
                resultData.setCount(count);
                resultData.setData(data);
                return resultData;
        }
        /**
         * 成功
         * @return
         */
        public static ResultData success(String errMsg, Object data){
                ResultData resultData = new ResultData();
                resultData.setStatus(ResultData.SUCCESS);
                resultData.setErrMsg(errMsg);
                resultData.setData(data);
                return resultData;
        }
        /**
         * 成功
         * @param errCode
         * @param errMsg
         * @param data
         * @return
         */
        public static ResultData success(String errCode,String errMsg,Object data){
                ResultData resultData = new ResultData();
                resultData.setStatus(ResultData.SUCCESS);
                resultData.setErrCode(errCode);
                resultData.setErrMsg(errMsg);
                resultData.setData(data);
                return resultData;

        }

        /**
         * 成功
         * @param errCode
         * @param errMsg
         * @return
         */
        public static ResultData fail(String errCode,String errMsg){
                ResultData resultData = new ResultData();
                resultData.setStatus(ResultData.FAIL);
                resultData.setErrCode(errCode);
                resultData.setErrMsg(errMsg);
                return resultData;
        }
        public static ResultData fail(CommonError commonErrorc){
                ResultData resultData = new ResultData();
                resultData.setStatus(ResultData.FAIL);
                resultData.setErrCode(commonErrorc.getErrorCode());
                resultData.setErrMsg(commonErrorc.getErrorMsg());
                return resultData;
        }
}

